package GUI;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilePermission;
import java.io.IOException;
import java.util.ArrayList;

import Control.CatchPhrase;
import Control.Character;
import Control.Import;


import processing.core.*;

public class Main extends PApplet{
	public int width=1024;
	public int height=768;
	int count=0;


	public void setup(){
		Utils.globalProcessing=this;
		Utils.gu = new GradientUtils();
		Utils.plotFont = loadFont("LucidaSans-48.vlw");
		Utils.plotBoldFont = loadFont("LucidaSans-Demi-48.vlw");
		size(width,height);


		Utils.characterSuggestionButtons = new ArrayList<CharacterButton>();
		Utils.catchPhraseSuggestionButtons = new ArrayList<CatchPhraseButton>();
		Utils.characterSelected = new ArrayList<Character>();
		Utils.catchPhrasesSelected = new ArrayList<CatchPhrase>();

		Utils.tagCloudActive=false;
		String[] iconNames = {"Fry.png", "Bender.png", "Leela.png","Farnsworth.png", "Zoidberg.png", "Amy.png", "Hermes.png"};

		//Utils.hs = new ScrollBarHorizontal(455, 190, 300, 20, 3*5+1);
		Utils.hs = new ScrollBarHorizontal(Utils.graphAreaLowerLeftX, 735, Utils.graphAreaUpperRightX-Utils.graphAreaLowerLeftX, 20, 3);
		
		Utils.colors = new int[7];
		Utils.colors[6]= color(25, 23, 23);
		Utils.colors[1]= color(113, 198, 198);
		Utils.colors[2]= color(174, 122, 208); //
		Utils.colors[3]= color(153, 153, 51); //
		Utils.colors[4]= color(56,110,186);//color(2, 13, 80);
		Utils.colors[5]= color(96, 177, 114); //
		Utils.colors[0]= color(126,24,41);//color(90, 88, 88);
		Utils.strokeWeight = new int[7];
		Utils.strokeWeight[0] = 2;
		Utils.strokeWeight[1] = 2;
		Utils.strokeWeight[2] = 2;
		Utils.strokeWeight[3] = 2;
		Utils.strokeWeight[4] = 2;
		Utils.strokeWeight[5] = 2;
		Utils.strokeWeight[6] = 2;
		
		
		try {
			
			
			Import.importFile("all_out.txt");
			Import.importFileWords("all_words.txt");
			Import.importDialog("all_dialogs.txt");
		} catch (FileNotFoundException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Utils.characterTextBox = new TextBox();
		Utils.catchPhrasesTextBox = new TextBox();
		Utils.ccip = new CharacterCatchInfoPanel();
		Utils.seip = new SeasonEpisodeInfoPanel();
		Utils.startEpisodeSelected= Utils.episodes.get(0);
		Utils.endEpisodeSelected= Utils.episodes.get(Utils.episodes.size()-1);
		Utils.graphArea = new GraphArea();
		createIconBarButtons(iconNames);
		Utils.infoButton = new InfoButton();
		Utils.infoButton.x=Utils.globalProcessing.width -525;
		Utils.infoButton.y=151;
		Utils.infoButton.label="?";
		frameRate(20);
	}
	public void createIconBarButtons(String[] iconNames){
		Utils.iconBarButtons = new ArrayList<IconButton>();
		for(int i=0;i<iconNames.length;i++){
			IconButton ib = new IconButton();
			ib.x=+i*Utils.BAR_ICON_WIDTH;
			ib.y=0;
			ib.setIcon(iconNames[i]);
			Utils.iconBarButtons.add(ib);
		}
	}
	public void drawBackGroundLayer(){

		background(Utils.backGroundColor);
		stroke(128);
		strokeWeight(2);
		line(width-250,0,width-250,height);
		line(width-250,height/2,width,height/2);
		line(0,190,width-250,190);
		line(0,112,width-250,112);

	}
	public void drawIconsBar(){
		for(int i =0;i<Utils.iconBarButtons.size();i++){
			Utils.iconBarButtons.get(i).draw();
		}


	}
	public void draw(){
		this.setVisible(true);
		textFont(Utils.plotFont,14);

		drawBackGroundLayer();
		drawLowerRightPanel();
		drawIconsBar();

		drawUpperRightPanel();
		drawSeasonEpisodeScrollBar();

		drawGraphArea();
		drawTextBoxes();
		
		
		//This is magic :)
		if(Utils.episodeHighLight!=null)
		Utils.episodeHighLight.drawInfoBox(Utils.episodeHighLight.x-(int)(Utils.globalProcessing.textWidth(Utils.episodeHighLight.label)
				)-20,Utils.episodeHighLight.y,(int)(Utils.globalProcessing.textWidth(Utils.episodeHighLight.label)) + 10,Utils.episodeHighLight.label);
		else if(Utils.characterInfoPanelHighLight!=null)
			Utils.characterInfoPanelHighLight.drawInfoBox(Utils.characterInfoPanelHighLight.x-(int)(Utils.globalProcessing.textWidth(Utils.characterInfoPanelHighLight.c.getName()))-20,
					Utils.characterInfoPanelHighLight.y-5,(int)(Utils.globalProcessing.textWidth(Utils.characterInfoPanelHighLight.c.getName()))
					+ 5,Utils.characterInfoPanelHighLight.c.getName());
		else if(Utils.catchPhraseInfoPanelHighLight!=null)
			Utils.catchPhraseInfoPanelHighLight.drawInfoBox(Utils.catchPhraseInfoPanelHighLight.x-(int)(Utils.globalProcessing.textWidth(Utils.catchPhraseInfoPanelHighLight.label))-20,
					Utils.catchPhraseInfoPanelHighLight.y,(int)(Utils.globalProcessing.textWidth(Utils.catchPhraseInfoPanelHighLight.label))
					+ 10,Utils.catchPhraseInfoPanelHighLight.label);


		Utils.infoButton.draw();
		
		if(Utils.infoButton.state){
		Utils.globalProcessing.noStroke();
		Utils.globalProcessing.fill(Utils.backGroundColor);
		Utils.globalProcessing.rectMode(Utils.globalProcessing.CORNERS);
		Utils.globalProcessing.rect(0, 191, width-253, 768);
		Utils.globalProcessing.fill(Utils.textColor);
		Utils.globalProcessing.textSize(25);
		Utils.globalProcessing.textAlign(Utils.globalProcessing.CENTER);
		//Utils.globalProcessing.text("",385,281);
		PImage image =Utils.globalProcessing.loadImage("Futurama.png");
		image.resize(300, 80);
		Utils.globalProcessing.image(image, 240, 241);
		Utils.globalProcessing.textSize(18);
		Utils.globalProcessing.textAlign(Utils.globalProcessing.LEFT);
		Utils.globalProcessing.text("Authors:",65,371);

		Utils.globalProcessing.textSize(15);
		Utils.globalProcessing.text("Andrea Cazzola",70,396);
		Utils.globalProcessing.text("Giovanni F. Del Nero",70,416);
		Utils.globalProcessing.text("Kevin McCray",70,436);
		
		
		Utils.globalProcessing.textSize(18);
		Utils.globalProcessing.text("Data:",65,471);
		
		Utils.globalProcessing.textSize(15);
		Utils.globalProcessing.text("http://theinfosphere.org/Episode_Transcript_Listing",70,496);
		
		Utils.globalProcessing.textSize(18);
		Utils.globalProcessing.text("Developed with:",65,531);
		
		Utils.globalProcessing.textSize(15);
		Utils.globalProcessing.text("Processing: http://processing.org/",70,556);


		}
	}

	private void drawGraphArea() {
		Utils.graphArea.draw();
	}
	public void drawSeasonEpisodeScrollBar(){
		Utils.hs.update();
		Utils.hs.display();
	}
	
	public void drawUpperRightPanel(){
		Utils.ccip.draw();
	}
	public void drawLowerRightPanel(){
		Utils.seip.draw();
	}


	public void drawTextBoxes(){
		fill(Utils.textColor);
	    textAlign(Utils.globalProcessing.LEFT);
		textFont(Utils.plotFont, 14);
		text("Character Name:",22,130);
		//Character Text Box
		Utils.characterTextBox.x = 22;
		Utils.characterTextBox.y = 145;
		Utils.characterTextBox.label = new String("Names");
		Utils.characterTextBox.width = 200;
		Utils.characterTextBox.draw();
		fill(Utils.textColor);
		textFont(Utils.plotFont, 14);
		text("Catch Phrase:",242,130);
		//Catch Phrases Text Box
		Utils.catchPhrasesTextBox.x = 242;
		Utils.catchPhrasesTextBox.y = 145;
		Utils.catchPhrasesTextBox.label = new String("CatchPhrases");
		Utils.catchPhrasesTextBox.width = 220;
		Utils.catchPhrasesTextBox.draw();

	}


	public void mousePressed(){
		if(Utils.characterTextBox.scrollBar.mouseOver())Utils.characterTextBox.scrollBar.mousePressed();
		else if(Utils.catchPhrasesTextBox.scrollBar.mouseOver())Utils.catchPhrasesTextBox.scrollBar.mousePressed();
		else if(Utils.ccip.scrollBar.mouseOver())Utils.ccip.scrollBar.mousePressed();
		else if(Utils.seip.scrollBar.mouseOver())Utils.seip.scrollBar.mousePressed();
		else if(Utils.graphArea.horizontalScrollBar.mouseOver())Utils.graphArea.horizontalScrollBar.mousePressed();
		else if(Utils.graphArea.verticalScrollBar.mouseOver())Utils.graphArea.verticalScrollBar.mousePressed();
		else if(Utils.characterTextBox.mouseOver()){
			Utils.characterTextBox.active=true;
			Utils.catchPhrasesTextBox.active=false;
		}
		else if(Utils.catchPhrasesTextBox.mouseOver()){
			Utils.characterTextBox.active=false;
			Utils.catchPhrasesTextBox.active=true;
		}
		else if((!Utils.characterTextBox.mouseOverSuggestions() || !Utils.characterTextBox.active)&&
				(!Utils.catchPhrasesTextBox.mouseOverSuggestions() || !Utils.catchPhrasesTextBox.active ))
		{	
			Utils.characterTextBox.active=false;
			Utils.catchPhrasesTextBox.active=false;
		}
	}
	public void mouseReleased(){
		Utils.characterTextBox.scrollBar.mouseReleased();
		Utils.catchPhrasesTextBox.scrollBar.mouseReleased();
		Utils.ccip.scrollBar.mouseReleased();
		Utils.seip.scrollBar.mouseReleased();
		Utils.hs.mouseReleased();
		Utils.graphArea.horizontalScrollBar.mouseReleased();
		Utils.graphArea.verticalScrollBar.mouseReleased();
	}

	public void mouseClicked(){
		if(Utils.tagCloudActive){
			count++;
			if(count >0)
				if(((Utils.globalProcessing.mouseX>Utils.graphAreaCenterX+Utils.graphAreaWidth/2) ||
						(Utils.globalProcessing.mouseX<Utils.graphAreaCenterX-Utils.graphAreaWidth/2) ||
						(Utils.globalProcessing.mouseY>Utils.graphAreaCenterY+Utils.graphAreaHeight/2)||
						(Utils.globalProcessing.mouseY<Utils.graphAreaCenterY-Utils.graphAreaHeight/2)) &&
						((Utils.globalProcessing.mouseX<Utils.tagCloudCWB.x) ||
						(Utils.globalProcessing.mouseX>Utils.tagCloudCWB.x+Utils.tagCloudCWB.width) ||
						(Utils.globalProcessing.mouseY<Utils.tagCloudCWB.y) ||
						(Utils.globalProcessing.mouseY>Utils.tagCloudCWB.y+Utils.tagCloudCWB.height) 
						)
						)
				{
					Utils.tagCloudCWB.selected=false;
					Utils.tagCloudActive=false;
					count=0;
				}
		}
		if(Utils.affineCharactersActive){
			count++;
			if(count >0)
				if(((Utils.globalProcessing.mouseX>Utils.graphAreaCenterX+Utils.graphAreaWidth/2) ||
						(Utils.globalProcessing.mouseX<Utils.graphAreaCenterX-Utils.graphAreaWidth/2) ||
						(Utils.globalProcessing.mouseY>Utils.graphAreaCenterY+Utils.graphAreaHeight/2)||
						(Utils.globalProcessing.mouseY<Utils.graphAreaCenterY-Utils.graphAreaHeight/2)) &&
						((Utils.globalProcessing.mouseX<Utils.tagCloudACB.x) ||
						(Utils.globalProcessing.mouseX>Utils.tagCloudACB.x+Utils.tagCloudACB.width) ||
						(Utils.globalProcessing.mouseY<Utils.tagCloudACB.y) ||
						(Utils.globalProcessing.mouseY>Utils.tagCloudACB.y+Utils.tagCloudACB.height) 
						)
						)
				{
			Utils.tagCloudACB.selected=false;
			Utils.affineCharactersActive=false;
			count=0;
		}
		}

	}

	//key Pressed input
	public void keyPressed() {
		if (keyCode==LEFT) {
		}
		if (keyCode==RIGHT) {
		}
		if (keyCode==UP) {
		}
		if (keyCode==DOWN) {
		}
		if(keyCode==TAB){
			if(Utils.characterTextBox.active){
				Utils.characterTextBox.active=false;
				Utils.catchPhrasesTextBox.active=true;
			}
			else if(Utils.catchPhrasesTextBox.active){
				Utils.characterTextBox.active=true;
				Utils.catchPhrasesTextBox.active=false;
			}
		}
	}
	public void keyTyped(){
		if(Utils.characterTextBox.active)Utils.characterTextBox.keyTyped();
		else if(Utils.catchPhrasesTextBox.active)Utils.catchPhrasesTextBox.keyTyped();
	}
	
	public static void main(String [] args){
		PApplet.main(new String[] { "--present", "GUI.Main" });
	}
	

	
}
